package com.xpec.c4.service.module.datacenter.player;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.xpec.c4.game.character.PlayerManager;
import com.xpec.c4.game.datamanager.ResultNo;
import com.xpec.c4.service.module.datacenter.DCAsyncCommand;

/**
 * 
 * @author yangbingyan
 *
 */
public class LoadPlayerDataAsync extends DCAsyncCommand{

	PlayerDataManager dataManager;
	
	/**
	 * 
	 */
	private static final Log log = LogFactory.getLog(LoadPlayerDataAsync.class);
	
	
	@Override
	public void asyncExecute() {
		
		dataInfo.setData(null);
		
		try {
			//dataInfo.setData((Object) PlayerDAO.loadPlayerByGuid(dataInfo.getKey()));
			dataInfo.setData((Object) PlayerManager.getInstance().loadPlayerFromDB(dataInfo.getKey(),
					this.getCurrentModuleServer()));			
		}
		//catch (InactiveToActiveException e) {
		catch (RuntimeException e) {
			log.error("cannot load player:" + dataInfo.getKey(), e);
//			throw new RuntimeException(e);
			resultCode = ResultNo.DB_OPERATION_FAILED;
			return;
		}		
		
		if (dataInfo.getData() != null) {
			resultCode = ResultNo.OK;
		} else {
			resultCode = ResultNo.DATA_NOT_FOUND;
			
			if (log.isInfoEnabled()) {
				log.info("dataInfo.getData() == null");
			}
		}
		
		
		return;
	}

	@Override
	public void execute() {		
		// callback
		if (dataManager != null) {
			dataManager.readDataCallback(resultCode, dataInfo, srcModuleID,
					dataInfo.getData(), true);
		}
	}
	
	public void setDataManager(PlayerDataManager dataManager) {
		this.dataManager = dataManager;
	}


}
